import routeDict from "@/dict/route"
import { isEmpty } from "../../utils/check"
export default {
  state: {
    tabbar: { list: [] },
    indexDeco: {},
    vipOneDeco: {},
    vipTwoDeco: {},
    problemDeco: {},
    personDeco: {},
    channelInfo: {},
    firstDecoRoute: null
  },
  getters: {
    tabbar: (state) => state.tabbar,
    indexDeco: (state) => state.indexDeco,
    vipOneDeco: (state) => state.vipOneDeco,
    vipTwoDeco: (state) => state.vipTwoDeco,
    problemDeco: (state) => state.problemDeco,
    personDeco: (state) => state.personDeco,
    channelInfo: (state) => state.channelInfo,
    firstDecoRoute: (state) => state.firstDecoRoute
  },
  mutations: {
    SET_TABBAR(state, data) {
      const tabbar = data.content?.find((item) => item.name === "tabbar")
      if (tabbar) state.tabbar = tabbar.data
    },
    SET_INDEX_DECO(state, data) {
      state.indexDeco = data
    },
    SET_VIP_ONE_DECO(state, data) {
      state.vipOneDeco = data
    },
    SET_VIP_TWO_DECO(state, data) {
      state.vipTwoDeco = data
    },
    SET_PROBLEM_DECO(state, data) {
      state.problemDeco = data
    },
    SET_PERSON_DECO(state, data) {
      state.personDeco = data
    },
    SET_CHANNEL_INFO(state, data) {
      state.channelInfo = data
    },
    SET_FIRST_DECO_ROUTE(state) {
      const routes = getCurrentPages()
      const currentRoute = routes[routes.length - 1]?.route
      if (state.tabbar.list.length) {
        const findCurrentRoute = routeDict.find((item) => item.route === currentRoute)
        if (state.tabbar.list.some((item) => item.value == findCurrentRoute.value)) {
          state.firstDecoRoute = findCurrentRoute
        } else {
          state.firstDecoRoute = routeDict.find((item) => item.value === state.tabbar.list[0].value)
        }
      }
    }
  },
  actions: {
    // 获取渠道信息
    getChannelInfo({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.shopPage.getChannelInfo({}, { queue: "async" })
          commit("SET_CHANNEL_INFO", res)
          if (res.color) commit("SET_THEME_COLOR", res.color)
          // #ifdef MP-WEIXIN
          const { appVersion } = uni.getSystemInfoSync()
          const wxInfo = uni.getAccountInfoSync()
          const wxVersion = wxInfo?.miniProgram.version
          if (isEmpty(wxVersion) && res.preReview == 1) commit("SET_IS_REVIEW_MODE", true)
          if (wxVersion && wxVersion != appVersion && res.preReview == 1) commit("SET_IS_REVIEW_MODE", true)
          // #endif
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    },
    // 获取首页装修数据
    getIndexDecoInfo({ commit, dispatch }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.shopPage.decoInfo({ type: "shopIndex" }, { queue: "async" })
          commit("SET_TABBAR", res)
          commit("SET_INDEX_DECO", res)
          commit("SET_FIRST_DECO_ROUTE", res)
          dispatch("decoJump")
          // #ifdef WEB
          dispatch("initShare", { title: res.config.shareTitle, desc: res.config.shareContent, imgUrl: res.config.shareImage, link: window.location.href })
          // #endif
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    },
    // 获取普通会员装修数据
    getVipOneDecoInfo({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.shopPage.decoInfo({ type: "vipOne" }, { queue: "async" })
          commit("SET_VIP_ONE_DECO", res)
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    },
    // 获取至尊会员装修数据
    getVipTwoDecoInfo({ commit, rootState }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.shopPage.decoInfo({ type: "vipTwo" }, { queue: "async" })
          if (rootState.weixin.isReviewMode == false) {
            commit("SET_VIP_TWO_DECO", res)
          }
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    },
    // 获取问题装修数据
    getProblemDecoInfo({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.shopPage.decoInfo({ type: "problem" }, { queue: "async" })
          commit("SET_PROBLEM_DECO", res)
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    },
    // 获取我的页面装修数据
    getMyDecoInfo({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.shopPage.decoInfo({ type: "my" }, { queue: "async" })
          commit("SET_PERSON_DECO", res)
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    },
    // 装修跳转
    decoJump({ state }) {
      if (state.firstDecoRoute) {
        const routes = getCurrentPages()
        const currentRoute = routes[routes.length - 1]?.route
        const findRoute = routeDict.find((item) => item.route === currentRoute)
        const isInTabbar = state.tabbar?.list.some((item) => item.value === findRoute.value)
        if (isInTabbar) return
        // #ifdef WEB
        const indexUrl = window.sessionStorage.getItem("indexUrl")
        const currentUrl = window.location.href.split("?")[0]
        if (indexUrl != currentUrl) return
        // #endif
        // if (state.firstDecoRoute.jumpType == "navigateTo") {
        //   uni.reLaunch({ url: `/${state.firstDecoRoute.route}` })
        // } else {
        //   uni[state.firstDecoRoute.jumpType]({ url: `/${state.firstDecoRoute.route}` })
        // }
      }
    }
  }
}
